import Vue from 'vue';
import VueRouter from 'vue-router';
import Cookies from 'js-cookie';
Vue.use(VueRouter);

import langs from '@/i18n/langs';
import { messages } from '@/i18n';

const Home = () => import('@/views/home/index');
// 订单数据
const DataStatistics = () => import('@/views/datastatistics/index');
// 案例订单
const CaseOrder = () => import('@/views/caseorder/index');
const reportPreview_phone = () => import('@/views/order/common/reportPreview_phone');

// 患者管理
const PatientManager = () => import('@/views/patient/index');
// 患者详情
const PatientDetail = () => import('@/views/patient/detail');
// 订单详情
const CaseOrderDetail = () => import('@/views/order/index');

// 合作伙伴
const Partner = () => import('@/views/partner/index');
// 机构成员
const OrganizationMember = () => import('@/views/organizationmember/index');
// 机构设备管理
const EquipmentManagement = () => import('@/views/equipmentmanagement/index');
// 机构设置
const OrganizationStructuring = () => import('@/views/organizationstructuring/index');
// 个人中心
const PersonCenter = () => import('@/views/person/index');
// 个人信息
const PersonInfo = () => import('@/views/person/personinfo/index');
// 账号密码
const PersonAccount = () => import('@/views/person/account/index');
// 偏好设置
const PersonPreference = () => import('@/views/person/preference/index');
// 机构信息
const OrganizationInfo = () => import('@/views/person/organizationinfo/index');
// 其他
// 工单
const WorkOrder = () => import('@/views/workorder/index');
// 通知
const Notification = () => import('@/views/notification/index');

// 设计方案
const Programme = () => import('@/views/programme/index');
// 版本管理
const orderManagement = () => import('@/views/order-management/index');
// 原厂版本上传
const VersionManagement_CurrVersion = () => import('@/views/version-management/currVersion.vue');
const VersionManagement_Oem = () => import('@/views/version-management/oem.vue');

// 版本日志管理
const VersionLogManagement = () => import('@/views/version-log-management');

// 客户端软件升级包
const ClientUpgradeVersion = () => import('@/views/clientUpgrade/clientVersion.vue');
const ClientUpgradeLog = () => import('@/views/clientUpgrade/clientLog.vue');
// 登录注册
const Login = () => import('@/views/login/login');
// 注册
const Register = () => import('@/views/register/register');
const SelectionRegister = () => import('@/views/register/selection');
const SelectionNextRegister = () => import('@/views/register/selectionnext');
const MobileTerminal = () => import('@/views/mobileTerminal/index'); // 这个好像没有用上
const Xieyi = () => import('@/views/xieyi/xieyi');

// 总后台
const Zhiduan = () => import('@/views/system/zhiduan');
const Leixing = () => import('@/views/system/leixing');
const Juese = () => import('@/views/system/juese');
const Yonghu = () => import('@/views/yonghu/index');
const Zhensuo = () => import('@/views/zhanghao/zhensuo');
const Jigongsuo = () => import('@/views/zhanghao/jigongsuo');
const Zhanghao = () => import('@/views/zhanghao/zhanghao');
const Gongdan = () => import('@/views/gongdan/index');
const Rongliang = () => import('@/views/rongliang/index');
const Xiaoxi = () => import('@/views/xiaoxi/index');

// 单独的页面
const OralReport = () => import('@/views/OralReport/index.vue');
const OrderPreview = () => import('@/views/order/common/preview.vue');
const orthodonticReport = () => import('@/views/orthodonticReport/index.vue');
// 用户下载页面
const userDownload = () => import('@/views/userDownload/index.vue');

// 口扫客户端
const OralScan = () => import('@/views/OralScan/index.vue');

// dentallmage
const dentallmage = () => import('@/views/dentallmage/index.vue');

// 更新记录
const updateLog = () => import('@/views/updateLog/index.vue');


const tableSweeper = () => import('@/views/tableSweeper/index.vue');

// 模型预览
const modelPreview = () => import('@/views/order/common/preview.vue');





const routes = [
  {
    path: '/',
    redirect: '/login',
  },

  {
    path: '/OrderPreview',
    component: OrderPreview,
  },
  {
    path: '/oralreport',
    component: OralReport,
  },
  {
    path: '/orthodonticReport',
    component: orthodonticReport,
  },
  {
    path: '/userDownload',
    component: userDownload,
  },

  {
    path: '/login',
    component: Login,
  },
  {
    path: '/xieyi',
    component: Xieyi,
  },
  {
    path: '/register',
    component: Register,
  },
  {
    path: '/selectionregister',
    component: SelectionRegister,
  },
  {
    path: '/selectionnextregister',
    component: SelectionNextRegister,
  },
  {
    path: '/mobileTerminal',
    component: MobileTerminal,
  },
  {
    // 案例订单详情
    path: '/reportPreview_phone',
    component: reportPreview_phone,
  },
  // 模型预览
  {
    path: '/preview',
    component: modelPreview,
  },
  {
    path: '/home',
    component: Home,
    redirect: '/datastatistics',
    children: [
      {
        // 数据统计
        path: '/datastatistics',
        component: DataStatistics,
      },
      {
        // 案例订单
        path: '/caseorder',
        component: CaseOrder,
      },
      {
        // 案例订单详情
        path: '/caseOrderDetail',
        component: CaseOrderDetail,
      },

      {
        // 患者管理
        path: '/patient',
        component: PatientManager,
      },
      {
        // 患者详情
        path: '/patient/detail',
        component: PatientDetail,
      },
      {
        // 合作伙伴
        path: '/partner',
        component: Partner,
      },
      {
        // 机构成员
        path: '/organizationmember',
        component: OrganizationMember,
      },
      {
        // 设备管理
        path: '/equipmentmanagement',
        component: EquipmentManagement,
      },
      {
        // 机构设置
        path: '/organizationstructuring',
        component: OrganizationStructuring,
      },
      {
        // 个人中心
        path: '/person',
        component: PersonCenter,
        redirect: '/personinfo',
        children: [
          {
            // 个人信息
            path: '/personinfo',
            component: PersonInfo,
          },
          {
            // 账号密码
            path: '/account',
            component: PersonAccount,
          },
          {
            // 偏好设置
            path: '/preference',
            component: PersonPreference,
          },
          {
            // 机构信息
            path: '/organizationinfo',
            component: OrganizationInfo,
          },
        ],
      },
      {
        // 工单
        path: '/workorder',
        component: WorkOrder,
      },

      {
        // 设计方案
        path: '/programme',
        component: Programme,
      },
      {
        // 通知
        path: '/notification',
        component: Notification,
      },
      // 总后台
      {
        path: '/zhiduan',
        component: Zhiduan,
      },
      {
        path: '/leixing',
        component: Leixing,
      },
      // 角色管理
      {
        path: '/juese',
        component: Juese,
      },
      // 用户管理
      {
        path: '/yonghu',
        component: Yonghu,
      },
      // 诊所管理
      {
        path: '/zhensuo',
        component: Zhensuo,
      },
      // 技工所管理
      {
        path: '/jigongsuo',
        component: Jigongsuo,
      },
      // 账号管理
      {
        path: '/dentalImageUser',
        component: Zhanghao,
      },
      // 工单管理
      {
        path: '/gongdan',
        component: Gongdan,
      },
      // 容量管理
      {
        path: '/rongliang',
        component: Rongliang,
      },
      {
        path: '/xiaoxi',
        component: Xiaoxi,
      },
      // 原厂版本管理
      {
        path: '/versionManagement_currVersion',
        component: VersionManagement_CurrVersion,
      },
      // oem版本管理
      {
        path: '/versionManagement_oem',
        component: VersionManagement_Oem,
      },
      // 订单文件下载
      {
        path: '/orderManagement',
        component: orderManagement,
      },
      // 设备升级日志
      {
        path: '/versionLogManagement',
        component: VersionLogManagement,
      },
      {
        path: '/oralScan',
        component: OralScan,
      },
      {
        path: '/dentallmage',
        component: dentallmage,
      },
      {
        path: '/updateLog',
        component: updateLog,
      },
      {
        // 案例订单详情
        path: '/tableSweeper',
        component: tableSweeper,
      },
      {
        path: '/clientUpgradeVersion',
        component: ClientUpgradeVersion,
      },
      {
        path: '/clientUpgradeLog',
        component: ClientUpgradeLog,
      },
    ],
  },


];

const router = new VueRouter({
  routes,
});

router.beforeEach((to, from, next) => {
  // 判断是否有加入机构
  function isJoined() {
    let bool = false;
    let user_jg_id = window.localStorage.getItem('user_jg_id');
    if (user_jg_id && user_jg_id !== 'null' && user_jg_id !== 'undefind') {
      // 没有机构
      bool = true;
    }
    return bool;
  }

  let cookies = Cookies.get('token');

  if (cookies) {
    if (to.path === '/login') {
      if (from.path === '/' && to.fullPath.includes('blz_email=blz_email')) {
        // 从邮件跳转详情过来
        localStorage.setItem(
          'blz_email',
          JSON.stringify({
            path: to.path,
            query: to.query,
          }),
        );
      } else {
        // 这里判断是否是后台账号
        let user_superAdmin = window.localStorage.getItem('user_superAdmin');
        if (user_superAdmin === '1') {
          // 如果是后台账号
          next('/zhiduan');
        } else {
          // 如果没有当前没有加入机构
          if (isJoined()) {
            next('/home');
          } else {
            if (to.path !== '/selectionregister') {
              next('/selectionregister'); // 只有在非 selectionregister 页面时才跳转
            } else {
              next();
            }
          }
        }
      }
    } else {
      if (!from.name) {
        // 用户手动输入 URL 进入
        if (isJoined()) {
          next();
        } else {
          if (to.path !== '/selectionregister') {
            next();
          } else {
            next();
          }
        }
      } else {
        // 用户非手动输入 URL 进入
        next();
      }
    }
  } else {
    next();
  }
});

router.afterEach((to, from) => {
  if (from.path === '/login') {
    localStorage.removeItem('blz_email');
  } else {
    const blzEmailStr = localStorage.getItem('blz_email');
    if (blzEmailStr !== undefined && blzEmailStr !== null && blzEmailStr !== '') {
      const { path } = JSON.parse(blzEmailStr);
      if (path !== from.path && to.path !== '/login') {
        setTimeout(() => {
          const token = Cookies.get('token');
          if (token !== undefined && token !== null && token !== '') {
            localStorage.removeItem('blz_email');
          }
        }, 6000);
      }
    }
  }
  let lang = 'cn';
  if (to.query.lang) {
    lang = to.query.lang ? to.query.lang : 'cn';
  } else {
    if (window.localStorage.getItem('lang')) {
      lang = window.localStorage.getItem('lang');
    } else {
      lang = 'cn';
    }
  }

  // let lang = to.query.lang ? to.query.lang : 'cn';

  let baseTitle = messages[lang]['brand'][Vue.prototype.$config.labelText];

  let prefixTitle = '';

  /**
   * 给标签栏标题设置成动态的
   */
  if (to.path === '/login') {
    // 登录
    prefixTitle = langs[lang]['login']['denglu'];
  } else if (to.path === '/xieyi') {
    // 协议
    prefixTitle = langs[lang]['login']['xieyi'];
  } else if (to.path === '/register') {
    // 注册
    prefixTitle = langs[lang]['login']['zhuche'];
  } else if (to.path === '/selectionregister') {
    // 创建/加入机构
    prefixTitle = langs[lang]['add']['add198'] + '/' + langs[lang]['add']['add199'];
  } else if (to.path === '/selectionnextregister') {
    /***
     * 加入机构 type=have
     * 创建机构 type=create
     * */
    prefixTitle = langs[lang]['add']['add199'];
  } else if (to.path === '/datastatistics') {
    // 数据统计
    prefixTitle = langs[lang]['routers']['data'];
  } else if (to.path === '/caseorder') {
    // 案例订单
    prefixTitle = langs[lang]['routers']['caseOrder'];
  } else if (to.path === '/caseOrderDetail') {
    // 案例订单详情
    prefixTitle = langs[lang]['data']['data29'] + langs[lang]['add']['add132'];
  } else if (to.path === '/reportPreview_phone') {
    prefixTitle = langs[lang]['caseorder']['anli82'];
  } else if (to.path === '/patient') {
    // 患者管理
    prefixTitle = langs[lang]['routers']['patient'];
  } else if (to.path === '/patient/detail') {
    // 患者详情
    prefixTitle = langs[lang]['add']['anli13'] + langs[lang]['add']['add132'];
  } else if (to.path === '/partner') {
    // 合作伙伴
    prefixTitle = langs[lang]['routers']['partner'];
  } else if (to.path === '/organizationmember') {
    // 机构成员
    prefixTitle = langs[lang]['routers']['members'];
  } else if (to.path === '/equipmentmanagement') {
    // 设备管理
    prefixTitle = langs[lang]['routers']['equipmentManagement'];
  } else if (to.path === '/organizationstructuring') {
    // 机构设置
    prefixTitle = langs[lang]['routers']['institutionSetting'];
  } else if (to.path === '/personinfo') {
    // 个人信息
    prefixTitle = langs[lang]['routers']['personInfo'];
  } else if (to.path === '/account') {
    // 账号密码
    prefixTitle = langs[lang]['routers']['personAccount'];
  } else if (to.path === '/preference') {
    // 偏好设置
    prefixTitle = langs[lang]['routers']['personPre'];
  } else if (to.path === '/organizationinfo') {
    // 机构信息
    prefixTitle = langs[lang]['routers']['personOriginationInfo'];
  } else if (to.path === '/workorder') {
    // 帮助
    prefixTitle = langs[lang]['add']['add113'];
  } else if (to.path === '/programme') {
    // 设计方案
    prefixTitle = langs[lang]['add']['add24'];
  } else if (to.path === '/notification') {
    // 公告通知
    prefixTitle = langs[lang]['add']['add154'];
  } else if (to.path === '/zhiduan') {
    // 工单类型管理
    prefixTitle = langs[lang]['add']['add117'];
  } else if (to.path === '/leixing') {
    // 工单类型管理（目前没有）
  } else if (to.path === '/juese') {
    // 角色管理
    prefixTitle = langs[lang]['home']['home16'];
  } else if (to.path === '/yonghu') {
    // 用户管理
    prefixTitle = langs[lang]['routers']['yonghu'];
  } else if (to.path === '/zhensuo') {
    // 诊所管理
    prefixTitle = langs[lang]['home']['home17'];
  } else if (to.path === '/jigongsuo') {
    // 技工所管理
    prefixTitle = langs[lang]['home']['home18'];
  } else if (to.path === '/gongdan') {
    // 工单管理
    prefixTitle = langs[lang]['routers']['gongdan'];
  } else if (to.path === '/rongliang') {
    // 容量管理
    prefixTitle = langs[lang]['routers']['rongliang'];
  } else if (to.path === '/xiaoxi') {
    // 目前没有
  } else if (to.path === '/orderManagement') {
    // 订单文件下载
    prefixTitle = langs[lang]['routers']['orderManage'];
  } else if (to.path === '/versionLogManagement') {
    // 设备升级日志
    prefixTitle = langs[lang]['routers']['router3'];
  } else if (to.path === '/oralreport') {
    prefixTitle = langs[lang]['oralReport']['oralReport_title'];
  } else if (to.path === '/OrderPreview') {
    prefixTitle = langs[lang]['caseorder']['anli20'];
  } else if (to.path === '/orthodonticReport') {
    prefixTitle = langs[lang]['orthodonticReport']['title'];
  } else if (to.path === '/userDownload') {
    prefixTitle = langs[lang]['userDownload']['userDownload8'];
  } else if (to.path === '/oralScan') {
    prefixTitle = langs[lang]['routers']['oralScan'];
  } else if (to.path === '/tableSweeper') {
    prefixTitle = langs[lang]['routers']['tableSweeper'];
  } else if (to.path === '/Dentallmage') {
    prefixTitle = langs[lang]['routers']['dentallmage'];
  } else if (to.path === '/clientUpgradeVersion') {
    prefixTitle = langs[lang]['routers']['version'];
  } else if (to.path === '/clientUpgradeLog') {
    prefixTitle = langs[lang]['routers']['versionLog'];
  } else if (to.path === '/versionManagement_currVersion') {
    prefixTitle = langs[lang]['routers']['router2'];
  } else if (to.path === '/versionManagement_oem') {
    prefixTitle = langs[lang]['routers']['router2'];
  } else if (to.path === '/dentalImageUser') {
    prefixTitle = langs[lang]['home']['home26'];
  } else if (to.path === '/updateLog') {
    prefixTitle = langs[lang]['home']['home27'];
  }

  document.title = prefixTitle + '-' + baseTitle;
});

export default router;
